﻿//=============================================================
// Copyright (C) 2011 深圳市中旭企业管理股份有限公司
// 作 者：孙德响
// 日 期：2011-11-8
// 描 述: 心得编辑界面
//
// 版 本：1.00
// 修改历史纪录
// 版 本  修改时间      修改人            修改内容
// 1.00   2011/11/8    孙德响           
//=============================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZX.Common;
using ZX.BLL;
using ZX.DBModule;
using System.Text.RegularExpressions;
using System.Text;
using ZX.Web.UI.Controls;
using ZX.BLL.Extend;
using ZX.EHR.Library;
using ZX.TR.Bussiness;
using ZX.TR.Library;
using ZX.SYS.Bussiness.Message;
using ZX.SYS.Library;
namespace ZX.WebApp.TR
{
    /// <summary>
    /// 心得
    /// </summary>
    public partial class CourceShareList : BasePage
    {
        BTrGainShare Btrac = new BTrGainShare();  //实例化业务处理类
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                //itype
                int itype = int.Parse(this.GetStringFromQueryString("itype", "2"));  //处理类型 
                DatUser du = (DatUser)System.Web.HttpContext.Current.Session["User"]; //用户ID
                string strTrActCID = this.GetStringFromQueryString("ID", "0000-0000-0000-000");   //主键ID    //主键ID   
                string strCourseShare = this.GetStringFromQueryString("CourseShare", "E_Menu");
                int type = int.Parse(this.GetStringFromQueryString("type", "1"));  //处理类型              
                if (du == null || du.UserID.ToString().Trim().Length == 0)
                {
                    HttpContext.Current.Response.Write("用户ID有误");
                    return;
                }
                if (strTrActCID == "0000-0000-0000-000" || strTrActCID == "")
                {
                    HttpContext.Current.Response.Write("信息有误");
                    return;
                }               
                if (itype == 1)
                {
                    #region ========心得管理1===========
                    
                    string strHId = this.GetStringFromQueryString("hid", ""); //检查人ID
                    #region  ============数据验证================                  
                    if (strCourseShare.Trim().Length > 300)
                    {
                        HttpContext.Current.Response.Write("点评字数不可以大于300");
                        return;
                    }
                    string[] arrstrtest = strCourseShare.Split('︾');
                    if (arrstrtest.Length > 0)
                    {
                        foreach (string strCourse in arrstrtest)
                        {
                            if (strCourse.Trim().Length > 0)
                            {
                                string[] strtype = strCourse.Split('︽'); //把类型和内容分开保存
                                if (strtype.Length < 2) continue;
                                if (strtype[1].Trim().Length > 200)
                                {
                                    HttpContext.Current.Response.Write("心得字数不可以大于200");
                                    return;
                                }
                            }
                        }

                    }
                    #endregion
                    TrGainShare ILTrGShare = Btrac.GetTrGainShare(" TrActivityCourseID='" + strTrActCID + "'" + " And  ShareUserid='" + du.UserID + "'");
                    if (type == 1)
                    {
                        #region   ==============新增心得==============
                        if (ILTrGShare != null)
                        {
                            HttpContext.Current.Response.Write("心得信息已经存在,不可以新增");
                            return;
                        }
                        else
                        {
                            TrGainShare TrGShare = new TrGainShare();
                            if (strTrActCID != null && strHId != null)
                            {
                                try
                                {
                                    if (strTrActCID == "" || strTrActCID == "0000-0000-0000-000")
                                    {
                                        HttpContext.Current.Response.Write("活动名称不能为空!");
                                        return;
                                    }
                                    else if (strHId == "")
                                    {
                                        HttpContext.Current.Response.Write("填写人不能为空!");
                                        return;
                                    }
                                    TrGShare.TrActivityCourseID = new Guid(strTrActCID);
                                    TrGShare.CheckUserid = new Guid(strHId);
                                    TrGShare.ShareUserid = du.UserID;
                                    TrGShare.ShareTime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd  HH:mm"));
                                    Btrac.entity = new TrGainShare();
                                    if (Btrac.Insert(TrGShare) == true)
                                    {
                                    }
                                    else
                                    {
                                        HttpContext.Current.Response.Write("课程活动保存失败");
                                        return;
                                    }
                                    if (strCourseShare.Trim().Length > 0)
                                    {
                                        string[] arrstr = strCourseShare.Split('︾');
                                        if (arrstr.Length > 0)
                                        {
                                            TrGainShare ILTrGShares = Btrac.GetTrGainShare(" TrActivityCourseID='" + strTrActCID + "'" + " And  ShareUserid='" + du.UserID + "'");  //获取插入主表的主键ID
                                            if (ILTrGShares != null)
                                            {
                                                int iOrder = 0;
                                               
                                                Guid TrGainShareID = ILTrGShares.TrGainShareID;
                                                foreach (string strCourse in arrstr)
                                                {
                                                    if (strCourse.Trim().Length > 0)
                                                    {
                                                        string[] strtype = strCourse.Split('︽'); //把类型和内容分开保存
                                                        if (strtype[1].Trim().Length == 0) continue;
                                                        TrGainShareDetail TrShareDetail = new TrGainShareDetail();
                                                       
                                                        TrShareDetail.TrGainShareID = TrGainShareID;
                                                        TrShareDetail.ShareType = strtype[0];
                                                        TrShareDetail.ShareContent = strtype[1];
                                                        TrShareDetail.Exp1 = iOrder++.ToString();
                                                        Btrac.entity = new TrGainShareDetail();
                                                        Btrac.Insert(TrShareDetail);
                                                    }
                                                }
                                                HttpContext.Current.Response.Write("1");
                                                
                                            }

                                        }
                                    }
                                }
                                catch
                                {
                                    HttpContext.Current.Response.Write("保存失败!");
                                }
                            }
                        }
                        #endregion
                    }
                    else if (type == 2)
                    {
                        #region  ==============更新心得==============
                        Btrac.entity = new TrGainShareDetail();
                        if (Btrac.GetShareDetail("TrGainShareID='" + strTrActCID + "'")!=null)
                        {
                            if (Btrac.delete("TrGainShareID='" + strTrActCID + "'") == false)                            
                            {
                                HttpContext.Current.Response.Write("更新心得出现异常");
                                return;
                            }
                        }
                        if (strCourseShare.Trim().Length > 0)
                        {
                            string[] arrstr = strCourseShare.Split('︾');
                            if (arrstr.Length > 0)
                            {
                                int iOrder = 0;
                                Guid TrGainShareID = new Guid(strTrActCID);
                                foreach (string strCourse in arrstr)
                                {
                                    if (strCourse.Trim().Length > 0)
                                    {
                                        string[] strtype = strCourse.Split('︽'); //把类型和内容分开保存
                                        if (strtype[1].Trim().Length == 0) continue;
                                        TrGainShareDetail TrShareDetail = new TrGainShareDetail();
                                        TrShareDetail.TrGainShareID = TrGainShareID;
                                        TrShareDetail.ShareType = strtype[0];
                                        TrShareDetail.ShareContent = strtype[1];
                                        TrShareDetail.Exp1 = iOrder++.ToString();
                                        Btrac.entity = new TrGainShareDetail();
                                        Btrac.Insert(TrShareDetail);
                                    }
                                }
                                HttpContext.Current.Response.Write("1");
                            }
                        }
                        #endregion
                    }
                    else if (type == 3)
                    {
                        #region  ===========新增/更新点评============
                        if (strTrActCID != null && strHId != null)
                        {
                            Btrac.entity = new TrGainShare();
                            TrGainShare GainShare = new TrGainShare();
                            GainShare.TrGainShareID = new Guid(strTrActCID);
                            GainShare.CheckContent = strCourseShare;
                            GainShare.CheckUserid = new Guid(strHId);
                            GainShare.CheckTime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd  HH:mm"));
                            if (Btrac.UpdateTrGainShare(GainShare) == true)
                            {
                                HttpContext.Current.Response.Write("1");
                            }
                            else
                            {
                                HttpContext.Current.Response.Write("点评失败");
                            }
                        }
                        #endregion
                    }
                    else if (type == 5)
                    {
                        #region ===============删除=================
                        if (strTrActCID != null && strTrActCID != "")
                        {
                            BaseBussnies buss = new BaseBussnies();
                            string sql = string.Format("Exec sp_delTrGainShare '{0}'", strTrActCID);
                            if (buss.RunSqlCommand(sql) == 0)
                            {
                                HttpContext.Current.Response.Write("删除失败!");
                            }
                            else
                            {
                                HttpContext.Current.Response.Write("1");
                            }
                        }
                        else
                        {
                            HttpContext.Current.Response.Write("删除失败!!");
                        }
                        #endregion
                    }
                    #endregion
                }
                else if(itype==2)
                {
                    #region ==========心得管理2============
                    if (type == 1)  //新增心得
                    {
                        #region ========新增心得=========
                        #region  ============数据验证================
                                              
                        string[] arrstrtest = strCourseShare.Split('︾');
                        if (arrstrtest.Length > 0)
                        {
                            if (arrstrtest[0].Trim().Length > 500)
                            {
                                HttpContext.Current.Response.Write("收获项字数不可以大于500字");
                                return;
                            }
                            if (arrstrtest[1].Trim().Length > 500)
                            {
                                HttpContext.Current.Response.Write("预期效果项字数不可以大于500字");
                                return;
                            }
                            if (arrstrtest[2].Trim().Length > 500)
                            {
                                HttpContext.Current.Response.Write("执行措施项字数不可以大于500字");
                                return;
                            }
                            if (arrstrtest[3].Trim().Length > 200)
                            {
                                HttpContext.Current.Response.Write("自罚承诺项字数不可以大于200字");
                                return;
                            }
                        }
                        
                        #endregion                        
                        TrStudyGain ILTrGShare = Btrac.GetTrStudyGain(" TrActivityCourseID='" + strTrActCID + "'" + " And  AddUser='" + du.UserID + "'");
                        if (ILTrGShare != null)
                        {
                            HttpContext.Current.Response.Write("此课程活动相关信息已经存在");
                            return;
                        }
                        Guid TrGainID =Guid.NewGuid();
                        TrStudyGain TrstudyGain = new TrStudyGain();
                        TrstudyGain.TrStudyGainID = TrGainID;
                        TrstudyGain.Gain = TrimString(arrstrtest[0].Trim().Replace("\r\n", ""));
                        TrstudyGain.PreResult = TrimString(arrstrtest[1].Trim().Replace("\r\n", ""));
                        TrstudyGain.Measure = TrimString(arrstrtest[2].Trim().Replace("\r\n", ""));
                        TrstudyGain.Punish = TrimString(arrstrtest[3].Trim().Replace("\r\n", ""));
                        TrstudyGain.TrActivityCourseID = new Guid(strTrActCID);
                        TrstudyGain.AddUser = du.UserID;
                        TrstudyGain.AddTime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd  HH:mm"));
                        Btrac.entity = new TrStudyGain();
                        if (Btrac.Insert(TrstudyGain) == true)
                        {
                           
                            #region   ======SendMessage======= 

                            Btrac.entity = new V_TrActivityCourseX();
                            V_TrActivityCourseX TrActivityCource = (V_TrActivityCourseX)Btrac.GetEntityById(strTrActCID);
                            if (TrActivityCource != null)
                            {

                                BDatMessageQueue target = new BDatMessageQueue(); // TODO: 初始化为适当的值
                                DatMessageQueue msg = DatMessageQueue.New(); // TODO: 初始化为适当的值
                                msg.MsgID = Guid.NewGuid();
                                msg.Title = "心得点评";
                                msg.MsgTemplate = "xxxx";
                                msg.MsgContent = "TR/EditTrStudyGain.aspx?ID=" + TrGainID.ToString() + "&type=3&entity=V_TrStudyGain";
                                msg.MessageType = "0";
                                msg.ExpectTime = Convert.ToDateTime(DateTime.Now.ToString());
                                msg.AddUser = du.UserID;
                                msg.QueryID = TrGainID;//关联信息来源的编号
                                IList<DatMessageReceiver> MessageReceiver = new List<DatMessageReceiver>(); // TODO: 初始化为适当的值
                                DatMessageReceiver mr1 = DatMessageReceiver.New();
                                mr1.MsItemID = Guid.NewGuid();
                                mr1.MsgID = msg.MsgID;
                                mr1.ReceiverID = TrActivityCource.PrincipalHrmanno;
                                mr1.ReceiverType = "U";
                                MessageReceiver.Add(mr1);
                                target.AddMessage(msg, MessageReceiver);
                                target.PushMessages( DateTime.Now);
                                HttpContext.Current.Response.Write("1");
                            }
                            else
                            {
                                HttpContext.Current.Response.Write("心得保存成功，但是发送点评人信息失败");
                            }
                            #endregion
                          
                            return;
                        }
                        else
                        {                          
                                HttpContext.Current.Response.Write("心得保存失败");
                                return;
                        }
                        #endregion
                    }
                    else if (type == 2)
                    {
                        #region =========更新心得===========
                        #region  ============数据验证================
                       
                        string[] arrstrtest = strCourseShare.Split('︾');
                        if (arrstrtest.Length > 0)
                        {
                            if (arrstrtest[0].Trim().Length > 500)
                            {
                                HttpContext.Current.Response.Write("收获项字数不可以大于500字");
                                return;
                            }
                            if (arrstrtest[1].Trim().Length > 500)
                            {
                                HttpContext.Current.Response.Write("预期效果项字数不可以大于500字");
                                return;
                            }
                            if (arrstrtest[2].Trim().Length > 500)
                            {
                                HttpContext.Current.Response.Write("执行措施项字数不可以大于500字");
                                return;
                            }
                            if (arrstrtest[3].Trim().Length > 200)
                            {
                                HttpContext.Current.Response.Write("自罚承诺项字数不可以大于200字");
                                return;
                            }
                        }

                        #endregion                        
                        TrStudyGain TrstudyGain = new TrStudyGain();
                        TrstudyGain.Gain = TrimString(arrstrtest[0].Trim().Replace("\r\n", ""));
                        TrstudyGain.PreResult = TrimString(arrstrtest[1].Trim().Replace("\r\n", ""));
                        TrstudyGain.Measure = TrimString(arrstrtest[2].Trim().Replace("\r\n", ""));
                        TrstudyGain.Punish = TrimString(arrstrtest[3].Trim().Replace("\r\n", ""));
                        TrstudyGain.TrStudyGainID = new Guid(strTrActCID);
                        TrstudyGain.AddUser = du.UserID;
                        TrstudyGain.AddTime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd  HH:mm"));
                        if (Btrac.UpdateTrStudyGain(TrstudyGain) == true)
                        {
                            HttpContext.Current.Response.Write("1");
                            return;
                        }
                        else
                        {
                            HttpContext.Current.Response.Write("更新心得");
                            return;
                        }
                        #endregion
                    }
                    else if (type == 3)
                    {
                        #region ========新增点评========
                        #region ====数据验证=====
                        
                        if (strCourseShare.Trim().Length > 500)
                        {
                            HttpContext.Current.Response.Write("点评不可以超过500字符");
                            return;
                        }
                        #endregion
                        TrStudyGainCheck ILTrGShare = Btrac.GetTrStudyGainCheck(" TrStudyGainID='" + strTrActCID + "'" + " And  AddUser='" + du.UserID + "'");
                        if (ILTrGShare != null)
                        {
                            HttpContext.Current.Response.Write("心得此用户点评信息已经存在！");
                            return;
                        }
                        Guid TrGainID = Guid.NewGuid();
                        TrStudyGainCheck Trstudygaincheck = new TrStudyGainCheck();
                        Trstudygaincheck.GainContent = strCourseShare;
                        Trstudygaincheck.TrStudyGainID = new Guid(strTrActCID);
                        Trstudygaincheck.AddUser = du.UserID;
                        Trstudygaincheck.AddTime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd  HH:mm"));
                        Btrac.entity = new TrStudyGainCheck();
                        if (Btrac.Insert(Trstudygaincheck) == true)
                        {
                            BDatMessagePool target = new BDatMessagePool(); // TODO: 初始化为适当的值                         
                            int expected = 1; // TODO: 初始化为适当的值
                            int actual;
                            actual = target.setSendState(new Guid(strTrActCID), du.UserID);
                            HttpContext.Current.Response.Write("1");
                            return;
                        }
                        else
                        {
                            HttpContext.Current.Response.Write("新增点评失败");
                            return;
                        }

                        #endregion
                    }
                    else if(type==4)
                    {
                        #region =========更新点评==========
                        #region ====数据验证=====
                        if (strCourseShare.Trim().Length > 500)
                        {
                            HttpContext.Current.Response.Write("点评不可以超过500字符");
                            return;
                        }
                        #endregion
                        TrStudyGainCheck Trstudygaincheck = new TrStudyGainCheck();
                        Trstudygaincheck.GainContent = strCourseShare;
                        Trstudygaincheck.TrStudyGainCheckID = new Guid(strTrActCID);
                        Trstudygaincheck.AddTime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd  HH:mm"));
                        if (Btrac.UpdateTrStudyGainCheck(Trstudygaincheck) == true)
                        {
                            HttpContext.Current.Response.Write("1");
                            return;
                        }
                        else
                        {
                            HttpContext.Current.Response.Write("更新点评失败");
                            return;
                        }
                        #endregion


                    }
                    else if (type == 5)
                    {
                        #region ===============删除=================
                        if (strTrActCID != null && strTrActCID != "")
                        {
                            string[] arrstrtest = strTrActCID.Split('︾');
                            int blResult = 0;
                            if (arrstrtest != null && arrstrtest.Length > 0)
                            {
                                for (int i = 0; i < arrstrtest.Length; i++)
                                {
                                    if (arrstrtest[i].Trim().Length == 0)  continue;
                                    BaseBussnies buss = new BaseBussnies();
                                    string sql = string.Format("Exec sp_TrDelStudyGain '{0}'", arrstrtest[i]);
                                    blResult = buss.RunSqlCommand(sql);
                                }
                            }
                            if (blResult  == 0)
                            {
                                HttpContext.Current.Response.Write("删除失败!");
                            }
                            else
                            {
                                HttpContext.Current.Response.Write("1");
                            }
                        }
                        else
                        {
                            HttpContext.Current.Response.Write("删除失败!!");
                        }
                        #endregion
                    }
                    #endregion

                }               
            }
            catch
            {
                HttpContext.Current.Response.Write("数据异常！");
            }
           
        }
        /// <summary>
        /// 去掉字符串的空格符
        /// </summary>
        /// <param name="TextString"></param>
        /// <returns></returns>
        public string TrimString(string TextString)
        {          
            string returnstr = string.Empty;
            CharEnumerator CEnumerator = TextString.GetEnumerator();
            while (CEnumerator.MoveNext())
            {
                byte[] array = new byte[1];
                array = System.Text.Encoding.ASCII.GetBytes(CEnumerator.Current.ToString());
                int asciicode = (short)(array[0]);
                if (asciicode != 32)
                {
                    returnstr += CEnumerator.Current.ToString();
                }
            }
            return returnstr;
        }

       
    }
}